package com.china08.yxyapi.api.home.activity.declaration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.huodong.declaration.YxyHuodongHrbDeclaration;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.declaration.YxyHuodongHrbDeclarationRepository;

/**
 * 添加我的宣言
 * 
 * @author ygl
 *
 */
@CrossOrigin
@RestController
public class ActivityHrbDeclarationCrtController extends YxyApiBaseController {

    @Autowired
    private YxyHuodongHrbDeclarationRepository yxyHuodongHrbDeclarationRepository;

    @RequestMapping(value = "/api_yw/home/activity/hrb/declaration/crt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        String userId = getProfile(request, response).getId();
        String schoolId = getProfile(request, response).getSchoolId();
        String schoolNick = getProfile(request, response).getSchoolName();
        String teacherNick = getProfile(request, response).getUserNick();
        String declaration = reqModel.declaration;

        RespModel model = new RespModel();
        if (null != declaration) {
            YxyHuodongHrbDeclaration hrbDeclaration = yxyHuodongHrbDeclarationRepository.findTopByOrderByOrderNumDesc();
            YxyHuodongHrbDeclaration yxyDeclaration = new YxyHuodongHrbDeclaration();
            yxyDeclaration.setDeclaration(declaration);
            yxyDeclaration.setIsBest(0);
            if (null == hrbDeclaration) {
                yxyDeclaration.setOrderNum(1);
            } else {
                yxyDeclaration.setOrderNum(hrbDeclaration.getOrderNum() + 1);
            }
            yxyDeclaration.setSchoolId(schoolId);
            yxyDeclaration.setSchoolNick(schoolNick);
            yxyDeclaration.setTeacherId(userId);
            yxyDeclaration.setTeacherNick(teacherNick);
            yxyHuodongHrbDeclarationRepository.save(yxyDeclaration);
            model.setMessage("添加宣言成功");
        }

        return model;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        // 教师宣言
        public String declaration;

    }

    protected static class RespModel extends YxyApiRespModel {
    }
}
